package remin;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.lang.*;

import javax.swing.JButton;
import javax.swing.JOptionPane;

public class DBCon implements ActionListener{
	
	public static Connection conn;
	Refr ref = new Refr();
	DelUp dup = new DelUp();

		
	public void actionPerformed(ActionEvent evt){
		String com = evt.getActionCommand();
				
		if(com =="Pripoj DB")
			{		
			
			      //  Establish a connection to a mSQL database using JDBC. 
		  			       
			              try {
			          	  
			           	  
			                  // Step 1: Load the JDBC driver. 
			                  Class.forName("oracle.jdbc.driver.OracleDriver"); 

			                  // Step 2: Establish the connection to the database. 
			                  String url = "jdbc:oracle:oci:@193.87.19.199:1521:kai"; 
			                  this.conn = DriverManager.getConnection(url,"stud31","stud31");
			                  RemGui.comm.setText("Spojenie nadviazane...");
			                  //System.out.println(conn);
			                 			                 
			                  RemGui.spris();                 
			                                 		                  

			                  //PreparedStatement ps = conn.prepareStatement("SELECT * from farby");
//			                 Statement s = conn.createStatement();
//			                 s.executeQuery("select * from reminder");
//			                 ResultSet rs = s.getResultSet();
//			                  
//			                  while(rs.next()){
//			                	  System.out.println(rs.getString(2));
//			                	  //System.out.println("Cyklus bezi");
//			                 }
//			                  System.out.println("Cyklus zbehol");
			                  			                  
			                  
			              }catch (Exception e){
			            	  //JOptionPane.showMessageDialog(null, e, "Chybova hlaska", 0);
			            	  /*System.err.println("Got an exception! "); 
			                  System.err.println(e.getMessage());*/
			            	  RemGui.comm.setText("Chyba pri pripojeni: "+ e.getMessage());
			            	  //e.printStackTrace();
			            	  
			              } 
			             
			          } 
		
	
		else if(com =="Pridaj udalost"){
			
			PridForm prid = new PridForm();
			
		}
		else if(com =="Odpoj DB") {
			if (conn != null){
				try{
				conn.close();
				RemGui.comm.setText("Uspesne odpojene...");
				RemGui.disab();
				
				}
				catch(Exception e){
					RemGui.comm.setText("Nepodarilo sa odpojit...");
				}
				
			}
		
			}
		else if(com =="Vypis obsah DB"){
			
			ref.refr();
			//tu bol refresh udalosti
		}
		else if(com =="Vymaz udalost"){
			try{
			dup.idRow();
			dup.delrow();
			//tu bol refresh udalosti
			}
			catch(Exception e){
				RemGui.comm.setText("Nie je vybrany riadok... Vyber riadok a akciu opakuj...");
			}
		}
		else if(com =="Uprav udalost"){
			try{
				dup.idRow();
				dup.napUF();
			}
			catch(Exception e){
				RemGui.comm.setText("Nie je vybrany riadok... Vyber riadok a akciu opakuj...");
			}
		}
	}
	
	public static Connection getConn(){
		return conn;
	}	
}


